package com.pengweiwei.androidzygote.functions.base.mvp;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.functions.httprequest.UserBean;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.LoadState;
import com.putong35.common.baseactfrag.activity.PullableListActivity;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.baseactfrag.mvp.IMvpListView;
import com.putong35.common.utils.EasyToast;

import java.util.List;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public class MyMvpListActivity extends PullableListActivity implements IMvpListView<UserBean>{

    ListActPresenter listActPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listActPresenter = new ListActPresenter(this);
        listActPresenter.attachView(this);

        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        listActPresenter.detachView();
    }

    private void init() {
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }


    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new TemplateListAdapter(this);
    }



    @Override
    public void onStartLoading() {
       /* String responseStr = AssetsUtil.getStringFromAssets(this, "list_ui_test_data.txt");
        Response parser = new Response(UserBean.class);
        parser.setJsonString(responseStr);
        List<UserBean> list = (List<UserBean>) parser.getDataListBean();
        if (list != null && list.size() > 0) {
            getAdapter().getDataListBean().addAll(list);
        }*/
       listActPresenter.loadNextPage();



    }

    @Override
    public void showToast(String msg) {
        EasyToast.showToast(this, msg);

    }

    @Override
    public void showToast(int strId) {
        EasyToast.showToast(this, strId);
    }

    @Override
    public void insertListItem(List<UserBean> list) {
        getAdapter().notifyItemInserted(list.size());
        setState(CompState.DATA);
        if (listActPresenter.hasNextPage()) {
            changeLoadState(LoadState.IDLE);
        } else {
            changeLoadState(LoadState.FINISH);
        }
        hideRefreshView();
    }



    @Override
    public void insertListItem(UserBean[] bean) {
        getAdapter().notifyItemInserted(bean.length);
        setState(CompState.DATA);
        if (listActPresenter.hasNextPage()) {
            changeLoadState(LoadState.IDLE);
        } else {
            changeLoadState(LoadState.FINISH);
        }
        hideRefreshView();
    }

    @Override
    public void removedListItem(List<UserBean> list) {
        getAdapter().notifyDataSetChanged();
    }

    @Override
    public void removedListItem(UserBean[] bean) {
        getAdapter().notifyDataSetChanged();
    }

    @Override
    public void clearList() {
        getAdapter().notifyDataSetChanged();
    }

    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<UserBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final UserBean bean = getData().get(i);
//            ImageLoader.with(RequestDemoListActivity.this).load(bean.getIconUrl()).into(holder.ivIcon).start();
            holder.tvText.setText(bean.getName());
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout lytMain;
            public TextView tvText;
            public ImageView ivIcon;

            public ItemViewHolder(View itemView) {
                super(itemView);
                lytMain = (RelativeLayout) itemView.findViewById(R.id.lytMain);
                ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
                tvText = (TextView) itemView.findViewById(R.id.tvText);
            }
        }
    }

}
